package com.example.zsf.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.zsf.R;
import com.example.zsf.adapter.DialogSelectListAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedListDialog extends Dialog {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Context context;
    private List<String> datas;
    private SelectedListener listener;

    public SelectedListDialog(@NonNull Context context,@NonNull List<String> datas,SelectedListener listener) {
        super(context, R.style.CustomDialog);
        this.context = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_list);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        DialogSelectListAdapter dialogSelectListAdapter = new DialogSelectListAdapter(R.layout.item_dialog_select_list,datas);
        recyclerView.setAdapter(dialogSelectListAdapter);
        dialogSelectListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (listener != null) {
                    listener.selected((String) adapter.getItem(position));
                    dismiss();
                }
            }
        });
    }

    public interface SelectedListener{
        void selected(String s);
    }


}
